package com.atguigu.gulimall.order.config;

import jdk.internal.org.objectweb.asm.commons.RemappingAnnotationAdapter;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.amqp.RabbitHealthIndicator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Configuration
public class MyRabbitConfig {





    private RabbitTemplate rabbitTemplate;



//    public MyRabbitConfig(RabbitTemplate rabbitTemplate){
//
//        this.rabbitTemplate=rabbitTemplate;
//
//    }


    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory){

        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        this.rabbitTemplate=rabbitTemplate;
        rabbitTemplate.setMessageConverter(messageConverter());
        initRabbitTemplate();

        return rabbitTemplate;
    }






    @Bean
    public MessageConverter messageConverter(){
        return  new Jackson2JsonMessageConverter();
    }





    public void initRabbitTemplate(){
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean b, String s) {


            }
        });
        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {//成功抵达queue的,没有回调,失败的才有回调
//            Message message, int replyCode, String replyText, String  exchange, String routingKey
            @Override
            public void returnedMessage(Message message, int i, String s, String s1, String s2) {

            }
        });




    }





}
